package com.thingcom.delinrobot.main.adddevice;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.DelinApplication;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.MyLog;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.thingcom.delinrobot.Common.Constant.MAC;
import static com.thingcom.delinrobot.Common.Constant.TOKEN;
import static com.thingcom.delinrobot.Common.Constant.UID;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/12/9
 */
public class DeviceApSuccessFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_ap_success)
    SimpleToolbar toolbarApSuccess;
    @BindView(R.id.btn_ap_success)
    Button btnApSuccess;
    private String uid;
    private String token;
    private String mac;

    public static DeviceApSuccessFragment newInstance() {
        Bundle args = new Bundle();
        DeviceApSuccessFragment fragment = new DeviceApSuccessFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GizWifiSDK.sharedInstance().setListener(mListener);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_ap_success, container, false);
        unbinder = ButterKnife.bind(this, view);
        uid = PreferencesUtil.getInstance(getContext()).getString(UID,null);
        token = PreferencesUtil.getInstance(getContext()).getString(TOKEN,null);
        mac = PreferencesUtil.getInstance(getContext()).getString(MAC,null);
        GizWifiSDK.sharedInstance().bindRemoteDevice(uid,token,mac, DelinApplication.PRODUCT_KEY,DelinApplication.PRODUCT_S,false);
        return view;
    }

    @OnClick(R.id.btn_ap_success)
    public void onViewClicked() {
        MyLog.i("4G绑定", "onViewClicked: "+"uid: "+uid+"token: " +token+"mac: "+mac);
        Intent intent = new Intent(getContext(), DeviceListActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        btnApSuccess.setClickable(false);
//      start(DeviceListFragment.newInstance(),2);

    }

    // 实现回调
    private GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didBindDevice(GizWifiErrorCode result, String did) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
               List<GizWifiDevice> device = GizWifiSDK.sharedInstance().getDeviceList();
               for (GizWifiDevice per:device){
                   if (per.isBind()&&per.getMacAddress().equals(mac)){
                       int robotChoose = PreferencesUtil.getInstance(getContext()).getInt(Constant.ROBOT_CHOOSE,0);
                       MyLog.i("配网","配网成功"+" device: "+per+"robotChoose: " + robotChoose);
                       per.setCustomInfo(String.valueOf(robotChoose),"");
                   }
               }
                // 绑定成功
                MyLog.i("4G绑定", "绑定成功: ");
            } else {
                // 绑定失败
                MyLog.i("4G绑定", "绑定失败: ");
                 }
            }
        };
}
